# -*- coding:utf-8 -*-
from urllib import request
import json
import requests


def get_token():
    client_id = 'eEiaGSVVtxYGD7wnGkGbIQGl'
    secret_key = 'VqqVGOym7LMDk2qYbqsPU5gk9WQYYGPA'
    url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' \
          + client_id +'&client_secret=' + secret_key
    response = request.urlopen(url)
    data = json.loads(response.read().decode('utf-8'))
    return data['access_token']


def check_text(text):
    request_url = "https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined"
    params = {"text": text}
    access_token = get_token()
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        return response.json()


print(check_text('ggg'))

